<template>
    <div class="app-wrap" >
        <slot></slot>
        <van-toast id="van-toast"></van-toast>
        <!--<app-loading  :show="isShowLoading"></app-loading>-->
        <van-notify id="van-notify"></van-notify>
        <van-dialog id="van-dialog"></van-dialog>
        <van-transition :show="isShowLoginPopup" name="fade">
            <login-popup
                    :show-close="!innerLogin"
                    @login-success="loginSuccessHandle"
                    @login-close="isShowLoginPopup = false"
                    :show-protocol="isShowProtocol"></login-popup>
        </van-transition>
    </div>
</template>

<script>
  import Vue from 'vue'
  import qs from 'qs'
  // import appLoading from './app-loading'
  import loginPopup from './login-popup'
  import Toast from '../native/components/vant/toast/toast'
  import Notify from '../native/components/vant/notify/notify';
  import Dialog from '../native/components/vant/dialog/dialog';

  export default {
    name: "app-wrapper",
    components: {
      loginPopup
    },
    props: {
      loading: {
        type: Boolean,
        default: false
      },
      // 是否能关闭登录弹窗
      closeLogin: {
        type: Boolean,
        default: false
      },
      // 是否在页面内登陆
      innerLogin: {
        type: Boolean,
        default: true
      },
      showLoginProtocol: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        isShowLoginPopup: false, // 是否展示底部弹窗
        isShowProtocol: this.showLoginProtocol,  // 是否展示政策
      }
    },
    watch: {
      '$store.getters.token'(val) {
        console.log('--watch-1')
        // 判断是否需要登录
        if(!val){
          console.log('--watch-2')
          // 判断是否在页面中登录，判断是否跳转到登录页面
          if (this.innerLogin) {
            console.log('--watch-3')
            // this.isShowLoading = false;
            this.isShowLoginPopup = true;
            this.isShowProtocol = false;
          } else if (this.$mp.page.route !== 'pages/login') {
            console.log('--watch-4')
            Megalo.redirectTo({
              url: '/pages/login?from=' + encodeURIComponent(this.$mp.page.route + '?' + qs.stringify(this.$mp.query))
            });
          }
        }
      }
    },
    methods: {
      getUserInfoHandle(data) {
        this.$emit('getUserInfo', data)
      },
      showLoading() {
        Toast.loading({
          message: '加载中...'
        });
      },
      hideLoading() {
        Toast.clear();
      },
      showProtocol() {
        this.isShowLoginPopup = true;
        this.isShowProtocol = true;
      },
      checkLogin() {
        console.log('login-0')
        if (!this.$store.getters.token) {
          // 判断是否在页面中登录，判断是否跳转到登录页面
          console.log('login-1')
          if (this.innerLogin) {
            this.isShowLoginPopup = true;
            this.isShowProtocol = false;
            console.log('login-2')
          } else if (this.$mp.page.route !== 'pages/login') {
            console.log('login-3')
            Megalo.redirectTo({
              url: '/pages/login?from=' + encodeURIComponent(this.$mp.page.route + '?' + qs.stringify(this.$mp.query))
            });
          }
        } else {
          console.log('login-4')
          this.isShowLoginPopup = false;
          this.$emit('login-success');
        }
      },
      loginSuccessHandle() {
        this.isShowLoginPopup = false;
        this.isShowProtocol = false;
        this.$emit('login-success');
      }
    },
    created() {
      Vue.prototype.$appWrap = this;
      Vue.prototype.$toast = Toast;
      Vue.prototype.$notify = Notify;
      Vue.prototype.$dialog = Dialog;

      this.checkLogin();
    }
  }
</script>

<style scoped lang="less">

</style>
